package com.example.javaui.slice;

import com.example.javaui.ResourceTable;
import com.example.javaui.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.TabList;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.TextAlignment;
import ohos.global.resource.Resource;

import java.util.ArrayList;

public class TabAbilitySlice extends AbilitySlice {

    // PagerSlider对象
    private PageSlider mPagerSlider;
    // 需要PagerSlider对象管理的用户界面列表
    private ArrayList<Component> mPageview;
    // TabList对象
    private TabList mTablist;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_tab);

        // 获取PagerSlider对象
        mPagerSlider = (PageSlider) findComponentById(ResourceTable.Id_pager_slider);

        // 创建PagerSlider所需要承载界面的列表
        mPageview = new ArrayList<Component>();
        mPageview.add(generateTextComponent("第1个界面"));
        mPageview.add(generateTextComponent("第2个界面"));
        mPageview.add(generateTextComponent("第3个界面"));

        // 实例化PageSliderProvider，为PagerSlider提供界面
        mPagerSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                // 界面数量
                return mPageview.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                Utils.log("createPageInContainer" + i);
                // 添加用户界面
                componentContainer.addComponent(mPageview.get(i));
                return mPageview.get(i);
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                Utils.log("destroyPageFromContainer" + i);
                // 添加用户界面
                componentContainer.removeComponent(mPageview.get(i));
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                // 判断createPageInContainer创建的界面与当前界面是否相同
                return component == o;
            }
        });
        // 获取TabList对象
        mTablist = (TabList) findComponentById(ResourceTable.Id_tablist);
        // 设置TabList的Tab总宽度
        mTablist.setTabLength(getResourceManager().getDeviceCapability().width);

        for(int i = 0; i < 3; i ++) {
            TabList.Tab tab = mTablist.new Tab(this);
            tab.setText("界面" + (i + 1));
            tab.setMarginsLeftAndRight(10, 10);
            tab.setTag(i);
            mTablist.addTab(tab);
        }

        mTablist.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                Utils.log("onSelected: " + tab.getText());
                mPagerSlider.setCurrentPage((int)tab.getTag());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
                Utils.log("onUnselected: " + tab.getText());
            }

            @Override
            public void onReselected(TabList.Tab tab) {
                Utils.log("onReselected: " + tab.getText());
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private Text generateTextComponent(String content) {
        Text text = new Text(this);
        text.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT));
        text.setTextAlignment(TextAlignment.CENTER);
        text.setText(content);
        text.setTextSize(80);
        return text;
    }
}
